﻿-- =============================================
-- Script Template
-- =============================================
USE [DB_Ex]
GO

/****** Object:  StoredProcedure [dbo].[sp_1C_Get_Returns_Params]    Script Date: 12/05/2011 10:39:53 ******/
SET ANSI_NULLS ON
GO

SET QUOTED_IDENTIFIER ON
GO

-- =============================================
-- Author:		<Author,,Name>
-- Create date: <Create Date,,>
-- Description:	<Description,,>
-- =============================================
CREATE PROCEDURE [dbo].[sp_1C_Get_Returns_Params] 
(
  @parDTFrom DateTime = NULL
, @parDTTo DateTime = NULL
, @parSklad varchar(100) = NULL
, @parCompany varchar(100) = NULL
)
AS
BEGIN

-- SET NOCOUNT ON added to prevent extra result sets from
-- interfering with SELECT statements.
SET NOCOUNT ON;

IF(@parDTFrom IS NOT NULL AND @parDTTo IS NOT NULL AND @parSklad IS NOT NULL AND @parCompany IS NOT NULL)
BEGIN
	SELECT [_Number], [DT] ,[Sklad] ,[Kontragent] ,[Sum] FROM [DB_Ex].[dbo].[vw_1C_Get_Returns]
	Where (DT >= @parDTFrom) AND (DT <= @parDTFrom) AND (Sklad = @parSklad) AND (Kontragent = @parCompany)
END
ELSE
BEGIN
	IF(@parDTFrom IS NULL AND @parDTTo IS NULL AND @parSklad IS NOT NULL AND @parCompany IS NOT NULL)
	BEGIN
		SELECT [_Number], [DT] ,[Sklad] ,[Kontragent] ,[Sum] FROM [DB_Ex].[dbo].[vw_1C_Get_Returns]
		Where (Sklad = @parSklad) AND (Kontragent = @parCompany)
	END
	ELSE
	BEGIN
		IF(@parDTFrom IS NULL AND @parDTTo IS NULL AND @parSklad IS NULL AND @parCompany IS NOT NULL)
		BEGIN
			SELECT [_Number], [DT] ,[Sklad] ,[Kontragent] ,[Sum] FROM [DB_Ex].[dbo].[vw_1C_Get_Returns]
			Where (Kontragent = @parCompany)	
		END
		ELSE
		BEGIN
			IF(@parDTFrom IS NULL AND @parDTTo IS NULL AND @parSklad IS NOT NULL AND @parCompany IS NULL)
			BEGIN
				SELECT [_Number], [DT] ,[Sklad] ,[Kontragent] ,[Sum] FROM [DB_Ex].[dbo].[vw_1C_Get_Returns]
				Where (Sklad = @parSklad)
			END
			ELSE
			BEGIN
				SELECT [_Number], [DT] ,[Sklad] ,[Kontragent] ,[Sum] FROM [DB_Ex].[dbo].[vw_1C_Get_Returns]
			END
		END
	END
END

END

GO


